//
//  VisitorView.swift
//  Super_WeiBo
//
//  Created by 梁超 on 16/3/15.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit

// 点击事件协议
protocol VisitorViewDelegate : NSObjectProtocol
{
    // 点击登录
    func pressLoginWillBtn()
    
    // 点击注册
    func pressRegisterWillBtn()
}


class VisitorView: UIView
{
    
    //  定义代理
    weak var delegate : VisitorViewDelegate?
    
    /**
     设置不同页面内的元素内容
     */
    func setupVisitorInfo(isHome : Bool, imageName: String, message : String)
    {
        iconView.hidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = message
        
        if isHome
        {
            startAnimation()
        }
    }
    /**
     登录点击事件
     */
    func pressLoginBtn()
    {
        delegate?.pressLoginWillBtn()
    }
    /**
     注册点击事件
     */
    func pressRegisterBtn()
    {
        delegate?.pressRegisterWillBtn()
    }
    
    override init(frame: CGRect) {
        
        super.init(frame : frame)
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // 设置约束
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        // 哪个控件的 什么属性 等于 另外一个控件 的什么属性  再乘于多少 再加上多少
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        registerBtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 10))
        
        loginBtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 10))
        
        maskBGView.xmg_Fill(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 懒加载
    /// 转盘
    private lazy var iconView : UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
        
    }()
    /// 图标
    private lazy var homeIcon : UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
        
    }()
    /// 文本
    private lazy var messageLabel : UILabel = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.text = "叫我去偶就哦请离开你未来可领取额领取金额看"
        return label
        
    }()
    /// 登录按钮
    private lazy var loginBtn : UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "pressLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    /// 注册按钮
    private lazy var registerBtn : UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "pressRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    /// 背景图
    private lazy var maskBGView : UIImageView = {
        
        let vi = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return vi
        
    }()
    
    //MARK: - 转盘动画
    func startAnimation ()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT // 循环次数
        anim.removedOnCompletion = false // 页面切换，动画不停
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
}
